/*
 * @Author: Paco
 * @Date:   2017-07-19
 * @lastModify 2017-07-24
 * +----------------------------------------------------------------------
 * | jqadmin [ jq酷打造的一款懒人后台模板 ]
 * | Copyright (c) 2017 http://jqadmin.jqcool.net All rights reserved.
 * | Licensed ( http://jqadmin.jqcool.net/licenses/ )
 * | Author: Paco <admin@jqcool.net>
 * +----------------------------------------------------------------------
 */

layui.define(['jquery', "jqtable", 'jqbind', 'jqajax', 'jqdate', 'upload', 'jqform'], function(exports) {
    var $ = layui.jquery,
        table = layui.jqtable,
        jqbind = layui.jqbind,
        form = layui.jqform,
        list = new table();
    list.init({ tplid: "#list-tpl" });
    top.global[list.options.dataName] = list;

    /**
     * 数据绑定后执行，此处为调出数据
     */
    form.afterBind = function(record, params, config) {
        if (record.roles == undefined) {
            $(config.form).find("input[lay-filter=role]").removeAttr("checked");
        } else {
            var ids;
            if (record) {
                if (typeof(record.roles) == "object") {
                    ids = record.roles;
                } else {
                    ids = record.roles.split(",");
                }

                $(config.form).find("input[lay-filter=role]").each(function(i, n) {
                    if ($.inArray(Number($(n).val()), ids) > -1) {
                        $(n).attr("checked", true);
                    }
                })
            }
        }
        form.render("checkbox");
    }

    /**
     * 弹窗前清空选中的角色
     * @param  {[type]} options [description]
     * @return {[type]}         [description]
     */
    jqbind.beforeModal = function(options) {
        $(options.content).find("input[lay-filter=role]").removeAttr("checked");
    }


    form.init();

    exports('admin', {});
});